package com.zxlspace.collection2;

import java.util.Iterator;

/**
 * 简化迭代器实现原理
 */
public class MyArrayList3 {

    private String[] array = new String[]{"a","b","c","d","e","f"};

    class MyIt implements Iterator {
        private int size = array.length;

        private int cusor = -1;  //游标位置

        //是否还有下一个元素
        public boolean hasNext() {
            return cusor+1 < size;
        }

        //返回下一个元素,游标往下移动一个位置
        public String next() {
            cusor++;
            return array[cusor];
        }

        public void remove() {

        }
    }

    public Iterator<String> iterator() {
        return new MyIt();
    }

    public static void main(String[] args) {
        MyArrayList3 myArrayList3 = new MyArrayList3();

        Iterator iterator = myArrayList3.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        Iterator iterator2 = myArrayList3.iterator();
        while (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }
    }
}
